package cn.com.bsoft.mobile.test


import cn.net.xyan.blossom.kotlin.base64
import org.junit.Test
import com.turo.pushy.apns.auth.ApnsSigningKey
import com.turo.pushy.apns.ApnsClientBuilder
import com.turo.pushy.apns.ApnsClient
import com.turo.pushy.apns.util.SimpleApnsPushNotification
import com.turo.pushy.apns.util.TokenUtil
import com.turo.pushy.apns.util.ApnsPayloadBuilder
import com.turo.pushy.apns.PushNotificationResponse







class ApnsTest{
    //@Test
    fun test1(){

        var file = javaClass.classLoader.getResourceAsStream("RJN.p8")
        println(file.readBytes().base64())

        var input = javaClass.classLoader.getResourceAsStream("RJN.p8")

        val apnsClient = ApnsClientBuilder()
                .setApnsServer(ApnsClientBuilder.PRODUCTION_APNS_HOST)
                .setSigningKey(ApnsSigningKey.loadFromInputStream(input,"5YM9U4V55H","6KD2JKP49T" /*"GX326C3552"*/))
                .build()

        val payloadBuilder = ApnsPayloadBuilder()
        payloadBuilder.setAlertBody("Example!")

        val payload = payloadBuilder.buildWithDefaultMaximumLength()
        val token = TokenUtil.sanitizeTokenString("2ed68731542d5b65988e3e68c879915b6f3a8cda021d1856a62bcb423644ba44")

        val pushNotification = SimpleApnsPushNotification(token, "com.cn.bsoft.apps.ios.MinhangJY.test1", payload)

        val sendNotificationFuture = apnsClient.sendNotification(pushNotification)

        val pushNotificationResponse = sendNotificationFuture.get()

        if (pushNotificationResponse.isAccepted) {
            System.out.println("Push notification accepted by APNs gateway.");
        } else {
            System.out.println("Notification rejected by the APNs gateway: " +
                    pushNotificationResponse.getRejectionReason());

            if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
                System.out.println("\t…and the token is invalid as of " +
                        pushNotificationResponse.getTokenInvalidationTimestamp());
            }
        }

    }
}